﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ChoWorkflowServer.Model;
using Cinchoo.Core;

namespace ChoWorkflowDashboard
{
    /// <summary>
    /// Interaction logic for AddTaskGroupWindow.xaml
    /// </summary>
    public partial class AddOrEditTaskGroupWindow : ChoBaseWindowEx
    {
        private ChoTaskGroupCategory _capturedObj;
        private readonly IChoDataModel _dataModel = ChoDataModelFactory.New();
        private readonly bool _isEditMode = false;
        private ChoTaskGroupCategory TaskGroupCategory
        {
            get;
            set;
        }

        private bool _isDataUpdated;
        internal bool IsDataUpdated
        {
            get { return _isDataUpdated; }
            set 
            { 
                if (value)
                    _isDataUpdated = value; 
            }
        }

        public AddOrEditTaskGroupWindow(ChoTaskGroupCategory taskGroupCategory, bool isEditMode = false)
        {
            ChoGuard.ArgumentNotNullOrEmpty(taskGroupCategory, "taskGroupCategory");

            _capturedObj = taskGroupCategory.Capture();
            _isEditMode = isEditMode;

            TaskGroupCategory = taskGroupCategory;
            TaskGroupCategory.IsDirty = false;
            this.DataContext = TaskGroupCategory;
            
            InitializeComponent();

            //txtTaskGroupName.IsEnabled = !_isEditMode;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            _promptSaveChanges = false;
            DialogResult = true;
            //this.Close();
        }

        protected override bool HasChanges
        {
            get
            {
                return TaskGroupCategory == null || TaskGroupCategory.IsDirty;
            }
        }

        protected override void RejectChanges()
        {
        }

        protected override bool UpdateChanges()
        {
            try
            {
                UpdateTaskGroup();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while updating task group.{0}{1}.".FormatString(Environment.NewLine, ex.Message),
                    Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);

                return false;
            }
        }

        private bool UpdateTaskGroup()
        {
            if (txtTaskGroupName.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("Enter valid task name.", Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            try
            {
                //Check for duplicate task name.
                if (_dataModel.IsTaskGroupExists(txtTaskGroupName.Text))
                {
                    if (_capturedObj.Name != txtTaskGroupName.Text)
                    {
                        MessageBox.Show("Duplicate task group found.", Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }
                }
                //Check for duplicate task name.
                int id = _dataModel.AddOrUpdateTaskGroup(txtTaskGroupName.Text, txtTaskGroupDesc.Text, _capturedObj.ParentGroupId, ChoUser.CurrentUser, Environment.MachineName);
                if (id <= 0)
                {
                    MessageBox.Show("Failed to add task group.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }
                TaskGroupCategory._id = id;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while adding task group.{0}{1}.".FormatString(Environment.NewLine, ex.Message),
                    Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            return true;
        }
    }
}
